package org.wmq.shopmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wmq.shopmanager.mapper.CategoryBrandMapper;
import org.wmq.shopmanager.service.BrandService;
import org.wmq.shopmanager.service.CategoryBrandService;
import org.wmq.shopmodel.DTO.product.CategoryBrandQueryDTO;
import org.wmq.shopmodel.Entity.product.Brand;
import org.wmq.shopmodel.Entity.product.CategoryBrand;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {
    private final CategoryBrandMapper categoryBrandMapper;
    private final BrandService brandService;

    @Override
    public IPage<CategoryBrand> getPageItem(CategoryBrandQueryDTO categoryBrandQueryDTO) {
        categoryBrandQueryDTO.setPageNo((categoryBrandQueryDTO.getPageNo() - 1)* categoryBrandQueryDTO.getPageSize());
        return this.categoryBrandMapper.getPageItem(categoryBrandMapper);
    }

    @Override
    public List<Brand> getBrandListByCategoryId(Long categoryId) {
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId,categoryId);
        queryWrapper.select(CategoryBrand::getBrandId);
        queryWrapper.orderByDesc(CategoryBrand::getId);
        List<CategoryBrand> list = this.list(queryWrapper);
        if (list != null && !list.isEmpty()){
            List<Long> brandIds = list.stream().map(CategoryBrand::getBrandId).toList();
            return brandService.listByIds(brandIds);
        }
        return null;
    }
}
